﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace CPETwit.WPFClient.CustomComponent
{
    public class LinkTextBlock : TextBlock
    {
        public static readonly DependencyProperty ClickCommandProperty =
        DependencyProperty.Register(
            "ClickCommand",
            typeof(ICommand),
            typeof(LinkTextBlock)
        );

        public static readonly DependencyProperty ClickCommandParameterProperty =
        DependencyProperty.Register(
            "ClickCommandParameter",
            typeof(ICommand),
            typeof(LinkTextBlock)
        );

        public ICommand ClickCommand
        {
            get { return (ICommand)GetValue(ClickCommandParameterProperty); }
            set { SetValue(ClickCommandProperty, value); }
        }
        public object ClickCommandParameter
        {
            get { return GetValue(ClickCommandParameterProperty); }
            set { SetValue(ClickCommandProperty, value); }
        }

        private Brush _brush;
        public LinkTextBlock()
        {
            Cursor = Cursors.Hand;
            Loaded += new RoutedEventHandler(LinkTextBlock_Loaded);
        }

        void LinkTextBlock_Loaded(object sender, RoutedEventArgs e)
        {
            _brush = Foreground;
        }

        protected override void OnMouseEnter(System.Windows.Input.MouseEventArgs e)
        {
            TextDecorations = System.Windows.TextDecorations.Underline;
            base.OnMouseEnter(e);
        }

        protected override void OnMouseLeave(System.Windows.Input.MouseEventArgs e)
        {
            TextDecorations = null;
            Foreground = _brush;
            base.OnMouseLeave(e);
        }

        protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            Foreground = new SolidColorBrush(Colors.Red);
            base.OnPreviewMouseLeftButtonDown(e);
        }

        protected override void OnPreviewMouseLeftButtonUp(MouseButtonEventArgs e)
        {
            Foreground = _brush;
            base.OnPreviewMouseLeftButtonDown(e);
        }

        protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
        {
            base.OnPreviewMouseDown(e);

            if(ClickCommand != null)
                ClickCommand.Execute(ClickCommandParameter);
        }

        public ICommand Command
        {
            get { throw new System.NotImplementedException(); }
        }

        public object CommandParameter
        {
            get { throw new System.NotImplementedException(); }
        }

        public IInputElement CommandTarget
        {
            get { throw new System.NotImplementedException(); }
        }
    }
}
